package com.cloud.alibaba.controller;

import com.cloud.alibaba.DubboApiService;
import com.cloud.alibaba.rpc.feign.FeignService;
import com.cloud.alibaba.service.ConsumerService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 刘志强
 * @date 2020/11/17 14:58
 */
@RestController
@Slf4j
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;

    @Reference(version = "1.0.0", check = false)
    private DubboApiService dubboApiService;

    @Autowired
    private FeignService feignService;

    @GetMapping("getUserName")
    public String getUserName() {
        return dubboApiService.getUserName();
    }

    @GetMapping("getUserNameTwo")
    public String getUserNameTwo(String userName) {
        return feignService.getUserName("token",userName);
    }


    @GetMapping("testDoubboSeata")
    @GlobalTransactional
    public String testDoubboSeata() {
        // 调用生产接口 添加用户
        dubboApiService.addUser("刘志强");
        // 调用自己的 添加用户接口
        consumerService.addUser("王妍");
        // 让方法异常
        int[] i = {1};
        log.info(String.valueOf(i[2]));
        return "观察 数据库是否有录入";
    }
}
